#include "Sensors.h"
#include <cstdlib> 
#include <iostream>

namespace Pennyworth
{
	namespace Sensors
	{
		Sensor::Sensor()
		{
			this->sensor_name = new std::wstring(L"Generic Sensor");
			this->sensor_description = new std::wstring(L"Sensor base class that does absolutely nothing.");

			this->sensor_identifier = rand();
		
			this->sensor_options = new std::map<std::wstring, std::wstring>();
			
			this->sensor_state = STATE_PAUSED;
		
			this->sensor_listeners = new std::vector<SensorListener>();
		}

		Sensor::Sensor(std::wstring * name, std::wstring * description, int identifier, std::map<std::wstring, std::wstring> * options)
		{
			this->sensor_name = new std::wstring(*name);
			this->sensor_description = new std::wstring(*description);

			this->sensor_identifier = identifier;
		
			if (options == NULL)
				this->sensor_options = new std::map<std::wstring, std::wstring>();
			else
				this->sensor_options = new std::map<std::wstring, std::wstring>(*options);
			
			this->sensor_state = STATE_PAUSED;
		
			this->sensor_listeners = new std::vector<SensorListener>();
		}

		Sensor::~Sensor()
		{
			delete sensor_name;
			delete sensor_description;
			delete sensor_options;
		
			// TODO: remove listeners first?
			delete sensor_listeners;
		}
		
		std::wstring * Sensor::name()
		{
			return this->sensor_name;
		}

		std::wstring * Sensor::description()
		{
			return this->sensor_description;
		}
	
		int Sensor::identifier()
		{
			return this->sensor_identifier;
		}

		SensorState Sensor::state()
		{
			return this->sensor_state;
		}
	
		void Sensor::setState(SensorState state)
		{
			this->sensor_state = state;
		}

		Reading * Sensor::mostRecentValue()
		{
			return NULL;
		}
		
		std::map<std::wstring, std::wstring> * Sensor::options()
		{
			return this->sensor_options;
		}
	
		void Sensor::setOptions(std::map<std::wstring, std::wstring> * options)
		{
			delete sensor_options;
		
			this->sensor_options = new std::map<std::wstring, std::wstring>(*options);
		}
		
		void Sensor::addListener(SensorListener * listener)
		{
			// TODO
		}
	
		void Sensor::removeListener(SensorListener * listener)
		{
			// TODO
		}

		std::vector<SensorListener> * Sensor::listeners()
		{
			return this->sensor_listeners;
		}
	}
}
